package com.graduation.springbootinit.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.model.entity.ActivityPart.OutfitWork;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OutfitWorkMapper extends BaseMapper<OutfitWork> {
    
    /**
     * 获取挑战的作品列表（按点赞数排序）
     */
    @Select("SELECT * FROM community_activities_outfit_work WHERE challenge_id = #{challengeId} ORDER BY like_count DESC")
    Page<OutfitWork> selectWorksByChallenge(Page<OutfitWork> page, @Param("challengeId") Long challengeId);
    
    /**
     * 获取用户的作品列表
     */
    @Select("SELECT * FROM community_activities_outfit_work WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<OutfitWork> selectWorksByUser(@Param("userId") Long userId);
    
    /**
     * 增加点赞数
     */
    @Update("UPDATE community_activities_outfit_work SET like_count = like_count + 1 WHERE id = #{id}")
    void incrementLikeCount(@Param("id") Long id);
    
    /**
     * 减少点赞数
     */
    @Update("UPDATE community_activities_outfit_work SET like_count = like_count - 1 WHERE id = #{id} AND like_count > 0")
    void decrementLikeCount(@Param("id") Long id);
    
    /**
     * 获取挑战的前三名作品
     */
    @Select("SELECT * FROM community_activities_outfit_work WHERE challenge_id = #{challengeId} ORDER BY like_count DESC LIMIT 3")
    List<OutfitWork> selectTopThreeWorks(@Param("challengeId") Long challengeId);
    
    /**
     * 更新作品排名和获得积分
     */
    @Update("UPDATE community_activities_outfit_work SET ranking = #{ranking}, points_received = #{points} WHERE id = #{id}")
    void updateRankAndPoints(@Param("id") Long id, @Param("ranking") Integer ranking, @Param("points") Integer points);
} 